package cn.edu.gdut.csq.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.gdut.csq.entity.Companyinfo;
import cn.edu.gdut.csq.entity.PageSet;
import cn.edu.gdut.csq.service.CompanyService;
import cn.edu.gdut.csq.util.ExcelUtil;
import cn.edu.gdut.csq.util.JsonUtils;

@Controller
@RequestMapping("company")
public class CompanyController {

	@Autowired
	private CompanyService companyService;

	@RequestMapping("/{queryType}/{state}/{pageNow}")
	public String getCompanysList(@PathVariable Integer pageNow,
			Integer pageSize, HttpServletRequest req, HttpServletResponse res,
			@PathVariable String queryType, @PathVariable Integer state) {

		System.out.println("pageNow " + pageNow);

		if (pageNow == null) {
			pageNow = 1;
		}
		if (pageSize == null) {
			pageSize = 20;
		}
		PageSet<Companyinfo> pagecompanys = companyService.getCompanys(pageNow,
				pageSize, queryType, state);
		// System.out.println(companys);
		req.getSession().setAttribute("pagecompanys", pagecompanys);
		req.getSession().setAttribute("queryType", queryType);
		req.getSession().setAttribute("state", state);
		return "company/companylist";
	}

	@RequestMapping("/{pageNow}")
	@ResponseBody
	public String getCompanys(Integer pageNow, Integer pageSize,
			HttpServletRequest req, HttpServletResponse res, String queryType,
			Integer state) {

		System.out.println("pageNow " + pageNow);

		if (pageNow == null) {
			pageNow = 1;
		}
		if (pageSize == null) {
			pageSize = 20;
		}
		PageSet<Companyinfo> pagecompanys = companyService.getCompanys(pageNow,
				pageSize, queryType, state);
		return JsonUtils.objectToJson(pagecompanys);
	}

	@RequestMapping("/today/{queryType}/{state}/{pageNow}")
	public String getCompanysByTime(@PathVariable Integer pageNow,
			Integer pageSize, HttpServletRequest req, HttpServletResponse res,
			@PathVariable String queryType, @PathVariable Integer state) {

		System.out.println("pageNow " + pageNow);

		if (pageNow == null) {
			pageNow = 1;
		}
		if (pageSize == null) {
			pageSize = 20;
		}
		String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System
				.currentTimeMillis()));
		System.out.println("company time : " + time);

		PageSet<Companyinfo> pagecompanys = companyService.getCompanyinfos(
				pageNow, pageSize, time, queryType, state);
		// System.out.println(companys);
		req.getSession().setAttribute("pagecompanys", pagecompanys);
		req.getSession().setAttribute("queryType", queryType);
		req.getSession().setAttribute("state", state);
		return "company/todaycompanylist";
	}

	@RequestMapping("/today/{queryType}/{state}/excel")
	public void getCompanyExcel(@PathVariable String queryType,
			@PathVariable Integer state, HttpServletRequest req,
			HttpServletResponse res) {

		System.out.println("starting export ...");

		String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System
				.currentTimeMillis()));
		System.out.println("company time : " + time);
		List<Companyinfo> list = companyService.exportCompanyinfos(time,
				queryType, state);

		LinkedHashMap<String, String> fieldMap = new LinkedHashMap<String, String>();

		fieldMap.put("companyName", "公司名称");
		fieldMap.put("queryTime", "添加时间");
		fieldMap.put("queryType", "公司来源");
		fieldMap.put("companyDetailUrl", "公司网址");

		String sheetName = new SimpleDateFormat("yyyy-MM-dd").format(new Date(
				System.currentTimeMillis()));

		int sheetSize = 65535;
		try {
			ExcelUtil.listToExcel(list, fieldMap, sheetName, sheetSize, res);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		System.out.println("end ..");
	}

	@RequestMapping("/state/{companyId}/0")
	@ResponseBody
	public String changeState(@PathVariable Integer companyId,
			HttpServletRequest req, HttpServletResponse res) {
		boolean b = companyService.changeState(companyId);
		if (b) {
			return JsonUtils.objectToJson("200");
		} else {
			return JsonUtils.objectToJson("400");
		}
	}

}
